<?php
namespace app\common\model;
use think\Model;
use think\Db;

/**
 * 区域代理返利
 */
class AdminBill extends Model {
	
	/**
	 * [addBill 添加交易明细]
	 * @param string  $city        [城市编码]
	 * @param integer $price       [返利金额]
	 * @param integer $foreign_key [对应相应的外键id]
	 * @param integer $type        [返利类型]
	 */
	public function addBill($city = '', $price = 0 , $foreign_key = 0 ,$type = 0){
		$city = trim($city);
		$price = floatval($price);
		if($city != '' && $price > 0){
			$admin = Db::table('admin_admin')->where("find_in_set(".$city.",area)")->find();
			if($admin){
				if(!Db::table('admin_admin')->where('id',$admin['id'])->setInc('balance',$price)){
					return false;
				}

				if(!$this->save([
					'foreign_key' => $foreign_key,
					'admin_id' => $admin['id'],
					'type' => $type,
					'price' => $price,
					'add_time' => time()
				])){
					return false;
				}

			}
		}
		return true;
	}
}
